package com.camemax.amqp.service.impl;

import com.camemax.amqp.service.RabbitmqConsumerService;
import com.camemax.amqp.service.RabbitmqPublisherService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Component
public class RabbitmqPublisherServiceImpl {
    @Autowired
    private RabbitTemplate template;

    @Service("fanoutPublisherService")
    class fanoutPublisher implements RabbitmqPublisherService {
        @Override
        public void publisher() {
            String fanoutExchange = "spring-boot-fanout-exchange";
            String routingKey = "";
            // 执行时，Rabbitmq三组件将会被创建
            template.convertAndSend(
                    fanoutExchange,
                    routingKey,
                    "spring-boot sends message!"
            );
            System.out.println("消息发送成功!");
        }
    }
//
//    @Service("directPublisherService")
//    class directPublisher implements RabbitmqPublisherService {
//        @Override
//        public void publisher() {
//            String directExchange = "spring-boot-direct-exchange";
//            String failRoutingKey = "fail";
//            String passRoutingKey = "pass";
//            String goodRoutingKey = "good";
//            // 执行时，Rabbitmq三组件将会被创建
//            template.convertAndSend(
//                    directExchange,
//                    failRoutingKey,
//                    "Your score is [FAIL]!"
//            );
//
//            template.convertAndSend(
//                    directExchange,
//                    goodRoutingKey,
//                    "Your score is [GOOD]!"
//            );
//            System.out.println("消息发送成功!");
//        }
//    }
//
//    @Service("topicPublisherService")
//    class topicPublisher implements RabbitmqPublisherService {
//
//        @Override
//        public void publisher() {
//            String topicExchange = "spring-boot-topic-exchange";
//            String topicRoutingKey = "com.students.name.test";
//            template.convertAndSend(
//                    topicExchange,
//                    topicRoutingKey,
//                    "Spring-Boot sending message!"
//            );
//            System.out.println("消息已发送！");
//        }
//    }
//
//    @Service("headersPublisherService")
//    class headersPublisher implements RabbitmqPublisherService {
//        @Override
//        public void publisher() {
//            String headersExchange = "spring-boot-headers-exchange";
//            String headersRoutingKey = "";
//            String headersMessage = "Spring-Boot sends message!";
//            MessageProperties headersMessageProperties = new MessageProperties();
//            headersMessageProperties.setContentType("UTF-8"); // 配置语言集
//            headersMessageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT); // 开启持久化
//            Map<String, Object> headersArgs = headersMessageProperties.getHeaders();
//            headersArgs.put("id","id001");
//            headersArgs.put("coffee","latte");
//            template.convertAndSend(
//                    headersExchange,
//                    headersRoutingKey,
//                    // 使用Message类封装待分发的消息以及指定好的分发格式
//                    new Message( headersMessage.getBytes(), headersMessageProperties)
//            );
//        }
//    }
}

